package com.colabo.j2ee.web.oaf.monitorperspective.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.colabo.j2ee.web.core.common.CommonConstants;
import com.colabo.j2ee.web.core.exception.ServiceException;
import com.colabo.j2ee.web.core.pkgenerator.IPKGenerator;
import com.colabo.j2ee.web.core.service.impl.DefaultServiceImpl;
import com.colabo.j2ee.web.core.vo.RecordObject;
import com.colabo.j2ee.web.oaf.monitorperspective.IScheduledTaskConfigService;
import com.colabo.j2ee.web.oaf.monitorperspective.IScheduledTaskManagerService;
import com.colabo.j2ee.web.oaf.vo.IPScheduledTaskVO;

/**
 * 定时任务配置服务的实现类
 */
public class ScheduledTaskConfigServiceImpl extends DefaultServiceImpl implements IScheduledTaskConfigService {
	protected final Log logger = LogFactory.getLog(ScheduledTaskConfigServiceImpl.class);

	public Integer[] saveData(Map objectList) throws ServiceException {
		// 插入成功的记录数、更新成功的记录数、删除成功的记录数
		int insertCount = 0;
		int updateCount = 0;
		int deleteCount = 0;

		// 删除主键列表、更新bean的列表、新增bean的列表
		String idList = (String) objectList.get(CommonConstants.ID_LIST);
		List insertBeanList = (ArrayList) objectList.get(CommonConstants.INSERT_BEAN_LIST);
		List updateBeanList = (ArrayList) objectList.get(CommonConstants.UPDATE_BEAN_LIST);

		IPScheduledTaskVO scheduleTask;
		Iterator it;
		IScheduledTaskManagerService schedule = (IScheduledTaskManagerService) getBean(IScheduledTaskManagerService.BEAN_ID);
		// 批量插入记录
		if (insertBeanList.size() > 0) {
			IPKGenerator keyService = (IPKGenerator) getBean(IPKGenerator.BEAN_ID);
			it = insertBeanList.iterator();
			while (it.hasNext()) {
				scheduleTask = (IPScheduledTaskVO) it.next();
				scheduleTask.setTaskKey(keyService.getUUIDKey());
				scheduleTask.setModified("y");
				this.save_Hibernate_Object(scheduleTask);
				insertCount++;
			}
		}

		// 批量更新记录
		if (updateBeanList.size() > 0) {
			it = updateBeanList.iterator();
			while (it.hasNext()) {
				scheduleTask = (IPScheduledTaskVO) it.next();
				String taskCode = scheduleTask.getTaskCode();
				scheduleTask.setModified("y");
				this.update_Hibernate_Object(scheduleTask);

				try {
					schedule.shutdown(taskCode);
				} catch (Exception e) {
					logger.warn("Can't start this task :" + scheduleTask.getTaskName());
					logger.warn("Task code:" + scheduleTask.getTaskCode());
					logger.warn("Task class:" + scheduleTask.getTaskConf());
					logger.warn(e);
				}
				updateCount++;
			}
		}

		// 批量删除记录
		if ((idList != null) && (idList.length() > 0)) {
			if (idList.endsWith(",")) {
				idList = idList.substring(0, idList.length() - 1);
			}
			StringTokenizer st = new StringTokenizer(idList, ",");
			while (st.hasMoreTokens()) {
				String idStr = st.nextToken();
				idStr = idStr.substring(1, idStr.length() - 1);
				scheduleTask = (IPScheduledTaskVO) this.findByID_Hibernate_Get(IPScheduledTaskVO.class, new String(idStr));
				try {
					schedule.shutdown(scheduleTask.getTaskCode());
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			//idList = idList.replaceAll("'", "");// delete (')char
			String sql = "DELETE FROM IP_SCHEDULE_TASK WHERE TASKKEY IN (" + idList + ")";
			//deleteCount = delete_Hibernate_ComposedQuery(sql, null);
			deleteCount = this.delete_JDBC(sql, null);
		}
		return new Integer[] { new Integer(insertCount), new Integer(updateCount), new Integer(deleteCount) };
	}

	public List<IPScheduledTaskVO> findAllScheduledTasks() throws ServiceException {
		return findAll_Hibernate_Object(IPScheduledTaskVO.class);
	}

	public List findByCode(String code) throws ServiceException {
		return this.findAll_JDBC("select * from IP_SCHEDULE_TASK where TASKCODE='" + code + "'");
	}
	
	public RecordObject findByPK(String pk) throws ServiceException {
		return this.findByID_Hibernate_Get(IPScheduledTaskVO.class, pk);
	}
}